﻿using PLC.Interface.Variables;
using PLCWpfTest.Common;
using PLCWpfTest.Common.Extensions;
using System.Threading.Tasks;
using System.Windows;
using YWM.Dto;

namespace PLCWpfTest.ViewModels
{
    public class VariableConfigViewModel : ManagementViewModel
    {
        private readonly IVariableAppService _variableAppService;

        public VariableResultRequestDto Query { get; set; }

        private PagedResultDto<VariableResultDto> _variablePaged;

        public PagedResultDto<VariableResultDto> VariablePaged
        {
            get { return _variablePaged; }
            set { SetProperty(ref _variablePaged, value); }
        }

        public VariableConfigViewModel(IVariableAppService variableAppService)
        {
            _variableAppService = variableAppService;
            Query = new VariableResultRequestDto();
        }

        protected override async Task LoadCommand_Executing(object parameter)
        {
            if (parameter != null)
            {
                Current = 1;
            }
            await SearchAsync();
        }

        public async Task SearchAsync()
        {
            Query.SkipCount = (Current - 1) * Query.MaxResultCount;
            VariablePaged = await _variableAppService.GetAll(Query);
        }

        protected override bool DeleteCommand_BeforeExecute(object parameter)
        {
            if (parameter == null)
            {
                this.ShowWarning("请选择一行删除");
                return false;
            }
            MessageBoxResult result = this.ShowQuestion(Lang["确定要删除吗"]);
            return result == MessageBoxResult.OK;
        }

        protected override async Task<IEntityDto> DeleteAsync(object parameter)
        {
            VariableDto variable = (VariableDto)parameter;
            await _variableAppService.Delete(variable);
            await SearchAsync();
            return variable;
        }
    }
}